'''
Author: Diana Tang
Date: 2025-04-06 00:53:32
LastEditors: Diana Tang
Description: some description
FilePath: /pub-scholar-assist/app/utils/config_manager.py
'''
import yaml
import os
import sys
from pathlib import Path

# 设置工程根目录
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
CONFIG_DIR = os.path.join(ROOT_DIR, "config")

class ConfigManager:
    def __init__(self, config_path=None):
        if config_path is None:
            self.config_path = os.path.join(CONFIG_DIR, "universities.yaml")
        else:
            self.config_path = config_path
            
        self.current_university = "tsinghua"  # 默认使用清华
        self.config = self._load_config()
    
    def _load_config(self):
        """加载配置文件"""
        try:
            with open(self.config_path, 'r', encoding='utf-8') as f:
                return yaml.safe_load(f)
        except Exception as e:
            print(f"加载配置文件失败: {str(e)}")
            return {"universities": {}}
    
    def get_university_config(self, university_code=None):
        """获取指定高校的配置"""
        if university_code is None:
            university_code = self.current_university
        
        if university_code not in self.config['universities']:
            raise ValueError(f"未知的高校代码: {university_code}")
        
        return self.config['universities'][university_code]
    
    def set_current_university(self, university_code):
        """设置当前使用的高校"""
        if university_code not in self.config['universities']:
            raise ValueError(f"未知的高校代码: {university_code}")
        
        self.current_university = university_code
        return self.get_university_config()
    
    def get_available_universities(self):
        """获取所有可用的高校列表"""
        return list(self.config['universities'].keys())
    
    def get_university_name(self, university_code):
        """获取高校名称"""
        return self.get_university_config(university_code)['full_name']
    
    def get_primary_color(self, university_code=None):
        """获取主色调"""
        return self.get_university_config(university_code)['color']['primary']
    
    def get_secondary_color(self, university_code=None):
        """获取次要色调"""
        return self.get_university_config(university_code)['color']['secondary']
    
    def get_accent_color(self, university_code=None):
        """获取强调色"""
        return self.get_university_config(university_code)['color']['accent']
    
    def get_description(self, university_code=None):
        """获取描述文本"""
        return self.get_university_config(university_code)['description'] 